from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *
import datetime
from django.db.models import Count, Sum
import math
from django.core.paginator import Paginator


class RecordView(APIView):

    def get(self, request):
        page_num = request.GET.get("page", 1)
        page_num = int(page_num)
        per_page = 3
        queryset = Record.objects.all()
        paginator = Paginator(queryset, per_page)

        page_data = paginator.get_page(page_num)

        ser = RecordSerializer(page_data, many=True)

        resp = {
            "data": ser.data,
            "max_page": paginator.num_pages
        }

        return Response(resp)

    def get2(self, request):

        page_num = request.GET.get("page", 1)
        page_num = int(page_num)
        per_page = 3

        print("page_num:", page_num)

        queryset = Record.objects.all()[(page_num - 1) * per_page:page_num * per_page]

        count = Record.objects.count()

        max_page = math.ceil(count / per_page)

        print("max_page", max_page)

        ser = RecordSerializer(queryset, many=True)
        resp = {
            "current_page": page_num,
            "data": ser.data,
            "max_page": max_page
        }

        return Response(resp)

    def get1(self, request):
        car_sn = request.GET.get("car_sn")
        if car_sn:
            queryset = Record.objects.filter(car_sn=car_sn)
        else:
            queryset = Record.objects.all()
        ser = RecordSerializer(queryset, many=True)
        return Response(ser.data)

    def post(self, request):
        car_sn = request.data.get("car_sn")
        Record.objects.create(car_sn=car_sn, start=datetime.datetime.now())
        return Response({"msg": "进入小区成功"})

    def put(self, request):
        id = request.data.get("id")
        record = Record.objects.get(id=id)  # 获取的但就是 model 记住只在id查询的时候用

        # 根据车牌号 查询 是不是业主
        if User.objects.filter(car_sn=record.car_sn):
            level = 1

        else:
            level = 2
        # 查询价格
        price = Price.objects.filter(level=level).first().price
        # 获取离开时间
        end = datetime.datetime.now()

        print(end)
        print(record.start)

        # 计算出 停车的时长,得出花费多少钱
        amount = (end - record.start).seconds / 3600 * price

        # 直接修改 model的属性值
        record.end = end
        record.amount = amount
        record.save()  # 保存到数据库

        return Response({"msg": "车辆离开"})


class UserView(APIView):

    def post(self, request):
        car_sn = request.data.get("car_sn")
        User.objects.create(car_sn=car_sn)
        return Response({"msg": "升级成功"})


class SearchView(APIView):
    def get(self, request):
        # 先统计 所有的车牌号的 停车次数  然后在筛选出大于三次

        queryset = Record.objects.annotate(count=Count("car_sn"), sum=Sum("amount"))

        for item in queryset:
            print(item.__dict__)

        # print(queryset)

        return Response({})
